/*

Input: tmp/wl_unique.dta [list of unique pollling locations with address]

Output: tmp/wl_unique_geocoded.dta [polling locations with geocodes]

Main task:
	> merge preprocessed geocodes of polling locations from 
		03_geocoded_addresses/wl_addresses_geocoded.dta
	
	OR
	
	Geolocate polling locations yourself (see details in do-file 03_geocode_ltw18.do)
	 
	To replicate the geocoding you need an API key from OpenCage and we recommend
		purchasing a request quota from OpenCage to accelarate the geocoding.
	See OpenCage for details: https://opencagedata.com/tutorials/geocode-in-stata

	If you want re-run the geocoding yourself set the global RERUN_GEOCODE_PP to 1 
		in _master.do and provide your API key.
		
	Otherwise, this dofile simply merges our geo-coordinates 
		03_geocoded_addresses/wl_addresses_geocoded.dta
	to the list of unique polling locations.

*/

 ** PULL: List of unique polling locations (across all elections)
 use "$tmp/wl_unique.dta", clear
  
 if $RERUN_GEOCODE_PP ==0 {
 	
	// MERGE geocodes to polling locations 
	merge m:1 wahllokal_id using "$geocod/wl_addresses_geocoded.dta", assert(3) nogen
	
	// save
	save "$tmp/wl_unique_geocoded.dta", replace
 }
 else if $RERUN_GEOCODE_PP ==1 {

	// gen city name
	gen stadt = "München"

	opencagegeo , key($api_key) countrycode(de) language(de) ///
				  city(stadt) street(strasse_wl) number(nummer_wl) 


	/* OUTPUT:

	OpenCage geocoded 313 of 313

	  g_quality |      Freq.     Percent        Cum.
	------------+-----------------------------------
	   6 street |          8        2.56        2.56 --> 8 instances where only street was found!
	   7 number |        305       97.44      100.00
	------------+-----------------------------------
		  Total |        313      100.00

	*/

	
	** Manually correct instances where only street (but not address nbr) was identified (g_quality=6)
	
	replace g_lat = "48.128493" if wahllokal_id=="bergamlaimstrasse140"			
	replace g_lon = "11.628849" if wahllokal_id=="bergamlaimstrasse140"

	replace g_lat = "48.153949" if wahllokal_id=="schwindstrasse19"
	replace g_lon = "11.563719" if wahllokal_id=="schwindstrasse19"

	replace g_lat = "48.169498" if wahllokal_id=="freseniusstrasse45"
	replace g_lon = "11.476888" if wahllokal_id=="freseniusstrasse45"

	replace g_lat = "48.1826237" if wahllokal_id=="schleissheimerstrasse336" 		
	replace g_lon = "11.5671737" if wahllokal_id=="schleissheimerstrasse336"

	replace g_lat = "48.1408276" if wahllokal_id=="hildegardhammbruecherstrasse5"
	replace g_lon = "11.4062949" if wahllokal_id=="hildegardhammbruecherstrasse5"  

	replace g_lat = "48.207662" if wahllokal_id=="eduardsprangerstrasse46"
	replace g_lon = "11.550730" if wahllokal_id=="eduardsprangerstrasse46"

	replace g_lat = "48.112215" if wahllokal_id=="karwendelstrasse39"
	replace g_lon = "11.537259" if wahllokal_id=="karwendelstrasse39"

	replace g_lat = "48.180676" if wahllokal_id=="untermenzingerstrasse1"
	replace g_lon = "11.505364" if wahllokal_id=="untermenzingerstrasse1"
	
    ** In one instance OpenCage identifies Schulstrasse 8 in wrong postcode (correct plz: 85757, wrong plz:80634)
	replace g_lat="48.21721" if wahllokal_id=="schulstrasse8"
	replace g_lon="11.46115" if wahllokal_id=="schulstrasse8"	

	// save: geocoded PP addresses
	drop nummer_wl strasse_wl stadt
	save "$tmp/wl_unique_geocoded.dta", replace
	
 }	
	
